/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.ventas;

import Algorithm.Algoritmo;
import Algorithm.EnvioPedidoAlg;
import Model.Cliente;
import Model.Pedido;
import Model.Producto;
import Model.PuntoMapa;
import Presenter.EnvioPedidoPresenter;
import Presenter.PedidoPresenter;
import static Presenter.PedidoPresenter.barrita;
import View.components.Index;
import View.components.PopUpBuscarCliente;
import View.components.PopUpBuscarProducto;
import View.components.PopUpPuntoMapa;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import Principals.Validadores;
import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;

/**
 *
 * @author Paola
 */
public class Frm_RegistrarPedido extends javax.swing.JDesktopPane {

    Validadores val = new Validadores();
    Cliente cliente;
    Producto producto;
    PuntoMapa punto;

    /**
     * Creates new form Frm_RegistrarPedido
     */
    public Frm_RegistrarPedido() {
        initComponents();
        val.desactivarAccionPegar(txtReferencia);
        EnvioPedidoPresenter.actualizarEnvioPedido() ; 
        btnCargarPedido.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnToolNuevo = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolGuardar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnToolLimpiar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlRegistrarPedido = new javax.swing.JPanel();
        lblCliente = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        jtbCliente = new javax.swing.JToolBar();
        btnBuscarCliente = new javax.swing.JButton();
        pnlDatosPedido = new javax.swing.JPanel();
        lblProducto = new javax.swing.JLabel();
        lblPrecioVenta = new javax.swing.JLabel();
        jtbProducto = new javax.swing.JToolBar();
        btnBuscarProducto = new javax.swing.JButton();
        txtProducto = new javax.swing.JTextField();
        txtPrecioVenta = new javax.swing.JTextField();
        pnlDireccion = new javax.swing.JPanel();
        jtbMapa = new javax.swing.JToolBar();
        btnBuscarPuntoMapa = new javax.swing.JButton();
        lblCoordX = new javax.swing.JLabel();
        lblDireccionEntrega = new javax.swing.JLabel();
        lblLatitudLongitud = new javax.swing.JLabel();
        txtPuntoMapa = new javax.swing.JTextField();
        txtDireccionEntrega = new javax.swing.JTextField();
        lblReferencia = new javax.swing.JLabel();
        txtReferencia = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        lblArchivo = new javax.swing.JLabel();
        txtNombreArchivo = new javax.swing.JTextField();
        btnExaminar = new javax.swing.JButton();
        btnCargarPedido = new javax.swing.JButton();
        barrita = new javax.swing.JProgressBar();

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);

        btnToolNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo.setEnabled(false);
        btnToolNuevo.setFocusable(false);
        btnToolNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolNuevo);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setEnabled(false);
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolModificar);

        btnToolGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Guardar.png"))); // NOI18N
        btnToolGuardar.setFocusable(false);
        btnToolGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolGuardarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolGuardar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setEnabled(false);
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolEliminar);
        jToolBar1.add(jSeparator2);

        btnToolLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpiar.setFocusable(false);
        btnToolLimpiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolLimpiarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolLimpiar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolBuscar);
        jToolBar1.add(jSeparator1);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolCancelar);
        jToolBar1.add(jSeparator3);

        pnlRegistrarPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro"));

        lblCliente.setText("Cliente*:");

        txtCliente.setEnabled(false);
        txtCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtClienteActionPerformed(evt);
            }
        });

        jtbCliente.setFloatable(false);
        jtbCliente.setRollover(true);
        jtbCliente.setBorderPainted(false);

        btnBuscarCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnBuscarCliente.setFocusable(false);
        btnBuscarCliente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscarCliente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });
        jtbCliente.add(btnBuscarCliente);

        pnlDatosPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Pedido"));

        lblProducto.setText("Producto*:");

        lblPrecioVenta.setText("Precio Venta*:");

        jtbProducto.setFloatable(false);
        jtbProducto.setRollover(true);
        jtbProducto.setBorderPainted(false);

        btnBuscarProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnBuscarProducto.setFocusable(false);
        btnBuscarProducto.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscarProducto.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBuscarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarProductoActionPerformed(evt);
            }
        });
        jtbProducto.add(btnBuscarProducto);

        txtProducto.setEnabled(false);
        txtProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtProductoActionPerformed(evt);
            }
        });

        txtPrecioVenta.setEnabled(false);

        javax.swing.GroupLayout pnlDatosPedidoLayout = new javax.swing.GroupLayout(pnlDatosPedido);
        pnlDatosPedido.setLayout(pnlDatosPedidoLayout);
        pnlDatosPedidoLayout.setHorizontalGroup(
            pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblProducto)
                    .addComponent(lblPrecioVenta))
                .addGap(6, 6, 6)
                .addGroup(pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPrecioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(jtbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );
        pnlDatosPedidoLayout.setVerticalGroup(
            pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlDatosPedidoLayout.createSequentialGroup()
                        .addGroup(pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblProducto)
                            .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlDatosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPrecioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPrecioVenta))))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pnlDireccion.setBorder(javax.swing.BorderFactory.createTitledBorder("Ubicación"));

        jtbMapa.setFloatable(false);
        jtbMapa.setRollover(true);
        jtbMapa.setBorderPainted(false);

        btnBuscarPuntoMapa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnBuscarPuntoMapa.setFocusable(false);
        btnBuscarPuntoMapa.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscarPuntoMapa.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBuscarPuntoMapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarPuntoMapaActionPerformed(evt);
            }
        });
        jtbMapa.add(btnBuscarPuntoMapa);

        lblCoordX.setText("Punto Mapa*:");

        lblDireccionEntrega.setText("Dirección*:");

        lblLatitudLongitud.setForeground(new java.awt.Color(102, 102, 102));
        lblLatitudLongitud.setText("(latitud, longitud)");

        txtPuntoMapa.setEnabled(false);
        txtPuntoMapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPuntoMapaActionPerformed(evt);
            }
        });

        txtDireccionEntrega.setEnabled(false);
        txtDireccionEntrega.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDireccionEntregaKeyTyped(evt);
            }
        });

        lblReferencia.setText("Referencia:");

        txtReferencia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtReferenciaKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnlDireccionLayout = new javax.swing.GroupLayout(pnlDireccion);
        pnlDireccion.setLayout(pnlDireccionLayout);
        pnlDireccionLayout.setHorizontalGroup(
            pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDireccionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDireccionLayout.createSequentialGroup()
                        .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCoordX)
                            .addComponent(lblDireccionEntrega))
                        .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlDireccionLayout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(pnlDireccionLayout.createSequentialGroup()
                                        .addComponent(lblLatitudLongitud)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(pnlDireccionLayout.createSequentialGroup()
                                        .addComponent(txtPuntoMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtbMapa, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))
                                .addContainerGap(154, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDireccionLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtReferencia)
                                    .addComponent(txtDireccionEntrega))
                                .addContainerGap())))
                    .addGroup(pnlDireccionLayout.createSequentialGroup()
                        .addComponent(lblReferencia)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        pnlDireccionLayout.setVerticalGroup(
            pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDireccionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCoordX)
                        .addComponent(txtPuntoMapa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jtbMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblLatitudLongitud)
                .addGap(13, 13, 13)
                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDireccionEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDireccionEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(pnlDireccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblReferencia)
                    .addComponent(txtReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlRegistrarPedidoLayout = new javax.swing.GroupLayout(pnlRegistrarPedido);
        pnlRegistrarPedido.setLayout(pnlRegistrarPedidoLayout);
        pnlRegistrarPedidoLayout.setHorizontalGroup(
            pnlRegistrarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRegistrarPedidoLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(lblCliente)
                .addGap(32, 32, 32)
                .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jtbCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(pnlDatosPedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlRegistrarPedidoLayout.setVerticalGroup(
            pnlRegistrarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRegistrarPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlRegistrarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtbCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlRegistrarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCliente)))
                .addGap(18, 18, 18)
                .addComponent(pnlDatosPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Carga individual", pnlRegistrarPedido);

        lblArchivo.setText("Archivo:");

        txtNombreArchivo.setEditable(false);

        btnExaminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Examinar.png"))); // NOI18N
        btnExaminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExaminarActionPerformed(evt);
            }
        });

        btnCargarPedido.setText("Cargar");
        btnCargarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarPedidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(lblArchivo)
                        .addGap(36, 36, 36)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtNombreArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnExaminar))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(barrita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(213, 213, 213)
                        .addComponent(btnCargarPedido)))
                .addContainerGap(129, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnExaminar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblArchivo)
                        .addComponent(txtNombreArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCargarPedido)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(barrita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(338, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Carga masiva", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        cambiarVista(new Frm_BuscarPedido()); // si se cancela se va a buscar
    }//GEN-LAST:event_btnToolCancelarActionPerformed

    private void btnToolLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolLimpiarActionPerformed
    }//GEN-LAST:event_btnToolLimpiarActionPerformed

    private void btnToolNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevoActionPerformed
    }//GEN-LAST:event_btnToolNuevoActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
        cambiarVista(new Frm_BuscarPedido());
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void btnToolGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolGuardarActionPerformed
        try {
            String direccion = txtDireccionEntrega.getText();
            String referencia = txtReferencia.getText();
            String cli = txtCliente.getText();
            String prod = txtProducto.getText();
            String precioVenta = txtPrecioVenta.getText();
            String ptomapa = txtPuntoMapa.getText();
            String msj_error = "";
            boolean error = false;

            if (direccion.isEmpty() || cli.isEmpty() || prod.isEmpty() || precioVenta.isEmpty() || ptomapa.isEmpty()) {
                msj_error = "Debe ingresar todos los campos obligatorios.";
                error=true;
            } 
        
            if (!referencia.isEmpty() && referencia.length() > 30) {
                if (error) {
                    msj_error = msj_error.concat("\n");
                }
                msj_error = msj_error.concat("El campo Referencia debe ser máximo de 30 caracteres.");
                error = true;
            }             
             if (error) {
                JOptionPane.showMessageDialog(this, msj_error);
            }
            
            else {
                int respuesta = JOptionPane.showConfirmDialog(this, "Está seguro de registrar el pedido", "Guardar", JOptionPane.OK_CANCEL_OPTION);
                switch (respuesta) {
                    case JOptionPane.OK_OPTION:
                        PedidoPresenter.registrarPedido(cliente, producto, punto, direccion,referencia);
                        JOptionPane.showMessageDialog(this, "Se registró el pedido.");
                        limpiar();
                        break;
                    case JOptionPane.CANCEL_OPTION:
                        break;
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "No se pudo realizar la transacción.");
            System.out.println("MENSAJE DE ERROR:" + e);
        }
    }//GEN-LAST:event_btnToolGuardarActionPerformed

    private void txtPuntoMapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPuntoMapaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPuntoMapaActionPerformed

    private void btnBuscarPuntoMapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarPuntoMapaActionPerformed
        new PopUpPuntoMapa((Index) this.getTopLevelAncestor());
    }//GEN-LAST:event_btnBuscarPuntoMapaActionPerformed

    private void txtProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtProductoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtProductoActionPerformed

    private void btnBuscarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarProductoActionPerformed
        new PopUpBuscarProducto((Index) this.getTopLevelAncestor());
    }//GEN-LAST:event_btnBuscarProductoActionPerformed

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        new PopUpBuscarCliente((Index) this.getTopLevelAncestor());
    }//GEN-LAST:event_btnBuscarClienteActionPerformed

    private void txtClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtClienteActionPerformed
    }//GEN-LAST:event_txtClienteActionPerformed

    private void txtDireccionEntregaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionEntregaKeyTyped
        val.validaCaracteresDirec(evt);
        val.validaMaxSize(evt, 40, txtDireccionEntrega.getText());
        txtDireccionEntrega.setTransferHandler(null);
    }//GEN-LAST:event_txtDireccionEntregaKeyTyped

    private void btnExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExaminarActionPerformed
        JFileChooser fileChooser = new JFileChooser();

        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
            String path = selectedFile.getAbsolutePath();
            txtNombreArchivo.setText(path);
            btnCargarPedido.setEnabled(true);
        } else if (status == JFileChooser.CANCEL_OPTION) {
            System.out.println(JFileChooser.CANCEL_OPTION);
            btnCargarPedido.setEnabled(false);
        }
    }//GEN-LAST:event_btnExaminarActionPerformed

    private void btnCargarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarPedidoActionPerformed
        String path = txtNombreArchivo.getText();
        PedidoPresenter.barrita = barrita ;
        PedidoPresenter.registrarPedido( path ) ;
        
        Algoritmo alg = new Algoritmo() ;
        alg.load() ;
        List<Double> lstFitnessSolucion = new ArrayList<Double>();
        List<EnvioPedidoAlg> lstEnvioPedido = alg.Genetico(lstFitnessSolucion);
        double cantidad = 100.0 / (double)85 ;
        for (int i = 0; i < lstEnvioPedido.size(); i++) {
            EnvioPedidoAlg envPed = lstEnvioPedido.get( i ) ;
            List<Pedido> lstPedido = envPed.getPedidos() ;
            int costoPedido = 0;
            for (int j = 0; j < lstPedido.size(); j++) costoPedido += lstPedido.get(j).getProducto().getPrecioLista().intValue() - lstPedido.get(j).getProducto().getPrecioCosto().intValue() ;
            envPed.setUtilidad( costoPedido - lstFitnessSolucion.get( i ) ) ;
            double value = (80+i) * cantidad ;
            barrita.setValue( (int) value ) ;
            Rectangle progressRect = barrita.getBounds() ;
            progressRect.x = 0 ;
            progressRect.y = 0 ;
            barrita.paintImmediately( progressRect );
            EnvioPedidoPresenter.registrarEnvioPedido( envPed , 1 ) ;
        }
        barrita.setValue( 100 ) ;
        Rectangle progressRect = barrita.getBounds() ;
        progressRect.x = 0 ;
        progressRect.y = 0 ;
        barrita.paintImmediately( progressRect ) ;
        
        JOptionPane.showMessageDialog(this, "Se registraron los pedidos con éxito");
        txtNombreArchivo.setText("");
        btnCargarPedido.setEnabled(false);
    }//GEN-LAST:event_btnCargarPedidoActionPerformed

    private void txtReferenciaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtReferenciaKeyTyped
        val.validaCaracteresDirec(evt);
        val.validaMaxSize(evt, 30, txtReferencia.getText());
        txtReferencia.setTransferHandler(null);
    }//GEN-LAST:event_txtReferenciaKeyTyped

    public void actualizarDatosCliente(Cliente cliente) {
        setCliente(cliente);
        txtCliente.setText(cliente.getApellidoPaterno() + " " + cliente.getApellidoMaterno() + ", " + cliente.getNombres());
    }

    public void actualizarDatosProducto(Producto producto) {
        setProducto(producto);
        txtProducto.setText(producto.getNombre());
        txtPrecioVenta.setText(producto.getPrecioLista().toString());
    }

    public void actualizarDatoPunto(PuntoMapa punto) {
        setPuntoMapa(punto);
        txtPuntoMapa.setText(punto.getLatitud() + " , " + punto.getLongitud());
        txtDireccionEntrega.setText(punto.getDireccion());
    }

    private void limpiar() {
        txtCliente.setText("");
        txtProducto.setText("");
        txtPrecioVenta.setText("");
        txtPuntoMapa.setText("");
        txtDireccionEntrega.setText("");
        txtReferencia.setText("");
    }

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    private Cliente getCliente() {
        return cliente;
    }

    private void setProducto(Producto prod) {
        this.producto = prod;
    }

    private Producto getProducto() {
        return producto;
    }

    private void setPuntoMapa(PuntoMapa puntoMapa) {
        this.punto = puntoMapa;
    }

    private PuntoMapa getPuntoMapa() {
        return punto;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar barrita;
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btnBuscarProducto;
    private javax.swing.JButton btnBuscarPuntoMapa;
    private javax.swing.JButton btnCargarPedido;
    private javax.swing.JButton btnExaminar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolGuardar;
    private javax.swing.JButton btnToolLimpiar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jtbCliente;
    private javax.swing.JToolBar jtbMapa;
    private javax.swing.JToolBar jtbProducto;
    private javax.swing.JLabel lblArchivo;
    private javax.swing.JLabel lblCliente;
    private javax.swing.JLabel lblCoordX;
    private javax.swing.JLabel lblDireccionEntrega;
    private javax.swing.JLabel lblLatitudLongitud;
    private javax.swing.JLabel lblPrecioVenta;
    private javax.swing.JLabel lblProducto;
    private javax.swing.JLabel lblReferencia;
    private javax.swing.JPanel pnlDatosPedido;
    private javax.swing.JPanel pnlDireccion;
    private javax.swing.JPanel pnlRegistrarPedido;
    private javax.swing.JTextField txtCliente;
    private javax.swing.JTextField txtDireccionEntrega;
    private javax.swing.JTextField txtNombreArchivo;
    private javax.swing.JTextField txtPrecioVenta;
    private javax.swing.JTextField txtProducto;
    private javax.swing.JTextField txtPuntoMapa;
    private javax.swing.JTextField txtReferencia;
    // End of variables declaration//GEN-END:variables
}
